#include "OptionsDialog.h"
#include <QtGui>
#include <cmath>

OptionsDialog::OptionsDialog() {
    setupUi(this);
    QSettings settings;
    linLocation->setText(settings.value("directory").toString());
    if (settings.value("mode") == "random") {
        radRandom->setChecked(true);
    } else if (settings.value("mode") == "sequential") {
        radSequential->setChecked(true);
    } else if (settings.value("mode") == "manual") {
        radManual->setChecked(true);
    }
    if (settings.value("display").toString() == "center") {
        radCenter->setChecked(true);
    } else if (settings.value("display") == "str") {
        radStretchLeave->setChecked(true);
    } else if (settings.value("display") == "ir") {
        radStretchIgnore->setChecked(true);
    } else if (settings.value("display") == "etr") {
        radStretchCrop->setChecked(true);
    } else if (settings.value("display").toString() == "tile") {
        radTile->setChecked(true);
    }
    int time = settings.value("time", 30).toInt();
    spnHours->setValue(time/60);
    spnMinutes->setValue(time%60);
    connect(buttonBox, SIGNAL(accepted()), SLOT(saveSettings()));
    connect(btnSetLocation, SIGNAL(clicked()), SLOT(setLocationClicked()));
}

void OptionsDialog::saveSettings() {
    QSettings settings;
    settings.setValue("directory", linLocation->text());
    settings.setValue("time", std::max(spnHours->value()*60 + spnMinutes->value(), 1));
    if (radRandom->isChecked()) {
        settings.setValue("mode", "random");
    } else if (radSequential->isChecked()) {
        settings.setValue("mode", "sequential");
    } else if (radManual->isChecked()) {
        settings.setValue("mode", "manual");
    }
    if (radCenter->isChecked()) {
        settings.setValue("display", "center");
    } else if (radStretchLeave->isChecked()) {
        settings.setValue("display", "str");
    } else if (radStretchIgnore->isChecked()) {
        settings.setValue("display", "ir");
    } else if (radStretchCrop->isChecked()) {
        settings.setValue("display", "etr");
    } else if (radTile->isChecked()) {
        settings.setValue("display", "tile");
    }
}

void OptionsDialog::setLocationClicked() {
    QString dir = QFileDialog::getExistingDirectory(this, tr("Get Location"), linLocation->text());
    if (!dir.isNull()) {
        linLocation->setText(QDir::convertSeparators(dir));
    }
}
